<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="mode==='add'?'新增打印机':'编辑打印机'"
    :close-on-click-modal="false"
    width="750px"
    top="10vh"
    @close="close"
  >
    <el-form ref="form" v-loading="loading" :model="form" :rules="rules" label-width="120px" size="small">
      <el-form-item label="所属店铺：" prop="shopId">
        <el-select v-model="form.shopId" filterable clearable placeholder="可通过名称搜索">
          <el-option
            v-for="item in shopOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="打印机名称：" prop="name">
        <el-input v-model="form.name" maxlength="15" show-word-limit placeholder="请输入" />
      </el-form-item>

      <el-form-item label="品牌：" prop="brandType">
        <el-radio-group v-model="form.brandType" @change="brandTypeChange">
          <el-radio
            v-for="(val,key,index) in brandOptions"
            :key="index"
            :label="Number(key)"
          >{{ val }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="打印纸尺寸：" prop="paperWidth">
        <el-radio-group v-model="form.paperWidth">
          <el-radio
            v-for="(val,index) in paperWidthOptions[form.brandType]"
            :key="index"
            :label="Number(val)"
          >{{ val+'毫米' }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="第三方SN号：" prop="snCode">
        <el-input v-model="form.snCode" maxlength="50" show-word-limit placeholder="请输入" />
      </el-form-item>

      <el-form-item v-if="form.brandType!==3001" label="秘钥：" prop="snKey">
        <el-input
          v-model.trim="form.snKey"
          :autosize="{ minRows: 3, maxRows: 6}"
          maxlength="200"
          show-word-limit
          type="textarea"
          placeholder="请输入秘钥"
        />
      </el-form-item>

      <el-form-item label="备注：" prop="tag">
        <el-input v-model="form.tag" maxlength="30" show-word-limit placeholder="说明备注" />
      </el-form-item>

      <!--<div v-if="form.brandType===3001">-->
      <!--  <el-form-item label="打印的单据：" prop="type">-->
      <!--    <div>-->
      <!--      <span>收款：</span>-->
      <!--      <el-checkbox v-model="form.isPayResult">静态二维码收款凭证</el-checkbox>-->
      <!--      <el-checkbox v-model="form.isLitePay">简易扫码设备收款凭证</el-checkbox>-->
      <!--      <el-checkbox v-model="form.isLiteSettlement">简易扫码设备结算凭证</el-checkbox>-->
      <!--    </div>-->
      <!--    <div>-->
      <!--      <div>-->
      <!--        <span>商城：</span>-->
      <!--        <el-checkbox v-model="form.isMallDelivery">配送拣货单</el-checkbox>-->
      <!--        <el-checkbox v-model="form.isMallPicking">自提拣货单</el-checkbox>-->
      <!--      </div>-->
      <!--      <div>-->
      <!--        <span style="visibility: hidden">预售：</span>-->
      <!--        <el-checkbox v-model="form.isMallPresellDelivery">配送拣货单(预售)</el-checkbox>-->
      <!--        <el-checkbox v-model="form.isMallPresellPicking">自提拣货单(预售)</el-checkbox>-->
      <!--      </div>-->
      <!--    </div>-->
      <!--  </el-form-item>-->
      <!--</div>-->

      <div>
        <el-form-item label="打印的单据：" prop="type">
          <div>
            <span>预订：</span>
            <el-checkbox v-model="form.isBookingConfirm">预订确认单</el-checkbox>
            <el-checkbox v-model="form.isBookingConsume">预订结算单</el-checkbox>
          </div>
          <div>
            <span>点餐：</span>
            <el-checkbox v-model="form.isCanyinProduce">制作单</el-checkbox>
            <el-checkbox v-model="form.isCanyinConsume">消费单</el-checkbox>
          </div>
          <div>
            <span>寄存：</span>
            <el-checkbox v-model="form.isDepositCommit">寄存提交单</el-checkbox>
            <el-checkbox v-model="form.isDepositTake">寄存支取单</el-checkbox>
          </div>
          <div>
            <span>收款：</span>
            <el-checkbox v-model="form.isPayResult">静态二维码收款凭证</el-checkbox>
            <el-checkbox v-model="form.isLitePay">简易扫码设备收款凭证</el-checkbox>
            <el-checkbox v-model="form.isLiteSettlement">简易扫码设备结算凭证</el-checkbox>
          </div>
          <div>
            <span>核券：</span>
            <el-checkbox v-model="form.isVerifyCoupon">核券凭证</el-checkbox>
          </div>
          <div>
            <div>
              <span>商城：</span>
              <el-checkbox v-model="form.isMallDelivery">配送拣货单</el-checkbox>
              <el-checkbox v-model="form.isMallPicking">自提拣货单</el-checkbox>
            </div>
            <div>
              <span style="visibility: hidden">预售：</span>
              <el-checkbox v-model="form.isMallPresellDelivery">配送拣货单(预售)</el-checkbox>
              <el-checkbox v-model="form.isMallPresellPicking">自提拣货单(预售)</el-checkbox>
            </div>
          </div>
        </el-form-item>
      </div>

      <el-form-item label="启用：" prop="status">
        <el-switch
          v-model="form.status"
          :active-value="1"
          :inactive-value="2"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" round @click="hide">取 消</el-button>
      <el-button size="small" round type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { editPrinter } from '@/api/mch/equipment'
import { allShopList } from '@/api/mch/shop'

export default {
  data() {
    return {
      // 校验字段相关
      rules: {
        shopId: [
          {
            required: true,
            message: '请选择所属店铺',
            trigger: ['change']
          }
        ],
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: ['blur', 'change']
          }
        ],
        brandType: [
          {
            required: true,
            message: '请选择品牌',
            trigger: ['change']
          }
        ],
        paperWidth: [
          {
            required: true,
            message: '请选择打印纸尺寸',
            trigger: ['change']
          }
        ],
        snCode: [
          {
            required: true,
            message: '请输入第三方SN号',
            trigger: ['blur', 'change']
          }
        ],
        snKey: [
          {
            required: true,
            message: '请输入秘钥',
            trigger: ['blur', 'change']
          }
        ],
        status: [
          {
            required: true,
            message: '请选择',
            trigger: ['blur', 'change']
          }
        ]
      },
      // 配置
      title: null,
      resolve: null,
      reject: null,
      brandOptions: {
        1001: '飞鹅',
        2001: '易联云',
        3001: '博实结'
      },
      paperWidthOptions: {
        1001: [58, 76, 80],
        2001: [58, 76, 80],
        3001: [58]
      },
      // 状态
      dialogVisible: false,
      loading: false,
      // 数据
      mode: 'add',
      shopOptions: [],
      form: {
        status: 1,
        shopId: '',
        name: '',
        brandType: 1001,
        paperWidth: 58,
        snCode: '',
        snKey: '',
        tag: '',
        isBookingConfirm: false,
        isBookingConsume: false,
        isCanyinConsume: false,
        isCanyinProduce: false,
        isDepositCommit: false,
        isDepositTake: false,
        isPayResult: false,
        isLitePay: false,
        isLiteSettlement: false,
        isVerifyCoupon: false,
        isMallDelivery: false,
        isMallPicking: false,
        isMallPresellDelivery: false,
        isMallPresellPicking: false
      }
    }
  },
  computed: {},
  watch: {},
  beforeCreate() {
  },
  created() {
  },
  beforeMount() {
  },
  mounted() {

  },
  beforeUpdate() {
  },
  update() {
  },
  activated() {
  },
  deactivated() {
  },
  beforeDestroy() {
  },
  destroyed() {
  },
  methods: {
    // 弹框相关
    open({ mode = 'add', row = null }) {
      this.getAllShopOptions()
      this.mode = mode
      if (mode === 'edit') this.form = Object.assign({}, row)
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    close() {
      this.form = {
        status: 1,
        shopId: '',
        name: '',
        brandType: 1001,
        paperWidth: 58,
        snCode: '',
        snKey: '',
        tag: '',
        isBookingConfirm: false,
        isBookingConsume: false,
        isCanyinConsume: false,
        isCanyinProduce: false,
        isDepositCommit: false,
        isDepositTake: false,
        isPayResult: false,
        isLitePay: false,
        isLiteSettlement: false,
        isVerifyCoupon: false,
        isMallDelivery: false,
        isMallPicking: false,
        isMallPresellDelivery: false,
        isMallPresellPicking: false
      }
      this.$refs.form.resetFields()
      this.loading = false
    },

    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // if (this.form.brandType === 3001) {
          //   this.form.isBookingConfirm = false
          //   this.form.isBookingConsume = false
          //   this.form.isCanyinConsume = false
          //   this.form.isCanyinProduce = false
          //   this.form.isDepositCommit = false
          //   this.form.isDepositTake = false
          //   this.form.isVerifyCoupon = false
          //   this.form.snKey = ''
          // }
          this.edit()
        }
      })
    },

    /**
     * 数据组
     */
    // 获取所有门店
    getAllShopOptions() {
      const params = {
        businessId: this.$route.query.mchId
      }
      this.loading = true
      allShopList({ params }).then(res => {
        this.shopOptions = res.data || []
      }).finally(() => {
        this.loading = false
      })
    },

    /**
     * 按钮组
     */
    // 编辑
    edit() {
      this.loading = true
      const params = Object.assign({}, this.form)
      if (this.mode === 'add') params.businessId = this.$route.query.mchId
      editPrinter({ data: params }).then(() => {
        this.$root.$emit('refreshMchEquipmentPrinterList')
        this.$notify({
          title: '成功',
          message: this.mode === 'add' ? '新增成功' : '更新成功',
          type: 'success',
          duration: 2000,
          offset: 40
        })
        this.hide()
      }).finally(() => {
        this.loading = false
      })
    },
    /**
     * 状态组
     */
    // 品牌变更
    brandTypeChange(val) {
      if (val === 3001) this.form.paperWidth = 58
    }

    /**
     * 方法组
     */
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-select {
  width: 100%;
}

.el-checkbox {
  width: 140px;
}
</style>
